﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.PushNotifications
{
    public class PushNotifications:IPushNotifications
    {
        public void AddPushNotification(int userId, Uri uri, ref Model.NutriLeafDBEntities context)
        {
            Model.PushNotification push = new Model.PushNotification();
            push.UserID = userId;
            push.Uri = uri.ToString();
            context.PushNotifications.AddObject(push);
            context.SaveChanges();
        }

        public void RemovePushNotification(int userid, ref Model.NutriLeafDBEntities context)
        {
            foreach (Model.PushNotification push in context.PushNotifications.Where(p => p.UserID == userid).ToArray()) {

                context.PushNotifications.DeleteObject(push);
                context.SaveChanges();
            }

        }

        public void RemovePushNotification(Model.PushNotification notification, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(notification);
            context.SaveChanges();
        }

        public void AddPushNotification(Model.PushNotification notification, ref Model.NutriLeafDBEntities context)
        {
            context.PushNotifications.AddObject(notification);
            context.SaveChanges();
        }

        public Uri GetPushNotification(int userId, ref Model.NutriLeafDBEntities context)
        {
            return new Uri(context.PushNotifications.Where(p => p.UserID == userId).First().Uri);
        }

        public Uri GetPushNotification(Model.User user, ref Model.NutriLeafDBEntities context)
        {
            return new Uri(context.PushNotifications.Where(p => p.UserID ==user.UserID).First().Uri);
        }

        public bool DoesPushNotificationExists(int userId, ref Model.NutriLeafDBEntities context)
        {
            if (context.PushNotifications.Where(p => p.UserID == userId).Count() != 0)
            {
                return true;
            }
            else
                return false;
        }

        public bool DoesPushNotifcationExists(Model.User user, ref Model.NutriLeafDBEntities context)
        {
            if (context.PushNotifications.Where(p => p.UserID == user.UserID).Count() != 0)
            {
                return true;
            }
            else
                return false;
        }
    }
}